package day16;

/** 需求 射击一个多线程编程模型，4个窗口共计售票100张
 * **/
public class TestTread {
    public static void main(String[] args) {
        TicketThread t1 = new TicketThread();
        TicketThread t2 = new TicketThread();
        TicketThread t3 = new TicketThread();
        TicketThread T4 = new TicketThread();

        t1.start();
        t2.start();
        t3.start();
        T4.start();
    }
}


//1自动逸多线程售票类
class TicketThread extends Thread{
    //3.定义成员变量，保存总票数
    static int tickets = 100;
    //2.重写父类的run(),里面是我们自定义的售票业务逻辑
    @Override
    public void run() {
        //4.循环卖票
        while(true){

            try {     //7给每个线程执行时增加一点时间，增加线程等待切换与出错的概率
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();

            }
            System.out.println(getName() + "=" + tickets--);   //有票就继续卖，打印县城名称后剩余票数
            if(tickets<=0) break;     //卖完票就退出循环
        }
    }
}